package com.shamar.blog.service.impl;


import com.shamar.blog.dao.BlogUserDao;
import com.shamar.blog.dao.UserEmailDao;
import com.shamar.blog.dto.BlogUserExecution;
import com.shamar.blog.dto.UserEmailExecution;
import com.shamar.blog.entity.BlogUser;
import com.shamar.blog.entity.UserEmail;
import com.shamar.blog.enums.BlogUserStateEnum;
import com.shamar.blog.enums.UserEmailStateEnum;
import com.shamar.blog.exceptions.BlogUserOperationException;
import com.shamar.blog.exceptions.UserEmailOperationException;
import com.shamar.blog.service.BlogUserService;
import com.shamar.blog.util.FileUtil;
import com.shamar.blog.util.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

//import com.imooc.o2o.dao.ShopAuthMapDao;
//import com.imooc.o2o.dao.ShopCategoryDao;
//import com.imooc.o2o.util.PageCalculator;

@Service
public class BlogUserServiceImpl implements BlogUserService {
	@Autowired
	private BlogUserDao blogUserDao;
	@Autowired
	private UserEmailDao userEmailDao;
//	@Autowired
//	private ShopAuthMapDao shopAuthMapDao;
//	@Autowired
//	private ShopCategoryDao shopCategoryDao;

	/**
	 *添加图片函数
	 * @param blogUser
	 * @param blogUserImgInputStream
	 * @param fileName
	 */
	private void addBlogUserImg(BlogUser blogUser, InputStream blogUserImgInputStream, String fileName) {
		//获取User图片目录的相对值路径
		//用日期作为图片存储的路径
		Calendar c = Calendar.getInstance();
		Integer current_date = c.get(Calendar.DAY_OF_MONTH);
		Long UserId = Long.valueOf(current_date);
		String dest = FileUtil.getBlogUserImagePath(UserId);//暂时设置为1
		String userImgAddr = ImageUtil.generateThumbnail(blogUserImgInputStream, fileName, dest);
//	String userImgAddr = shopImgInputStream.getAbsolutePath();
		blogUser.setImgaddr(userImgAddr);
	}

	@Override
	public BlogUserExecution getBlogUserList(BlogUser blogUserCondition, int pageIndex,
                                             int pageSize) {
		//将pageindex转换为rowindex
//		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据blogUserCondition查找blogUserList
//        if (pageIndex == null){
//            pageIndex = 1;
//        }
		List<BlogUser> blogUserList = blogUserDao.queryUserList(blogUserCondition,pageIndex,pageSize);
		//依据相同的查询条件,返回店铺总数
		int count = blogUserList.size();
		BlogUserExecution be = new BlogUserExecution();
		if (blogUserList != null) {
			be.setBlogUserList(blogUserList);
			be.setCount(count);
		} else {
			be.setState(BlogUserStateEnum.BANNED.getState());
		}
		return be;
	}

	/**
	 * 根据条件查找用户
	 * @param blogUserCondition
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@Override
	public BlogUserExecution getBlogUserByCondition(BlogUser blogUserCondition, int pageIndex, int pageSize) {
		BlogUser blogUser = blogUserDao.selectByBlogUserCondition(blogUserCondition,pageIndex,pageSize);
		BlogUserExecution be = new BlogUserExecution();
		if (blogUser != null){
			be.setBlogUser(blogUser);
			be.setState(BlogUserStateEnum.NORMAL.getState());
			return be;
		}
		be.setState(BlogUserStateEnum.NO_USER.getState());
		be.setStateInfo(BlogUserStateEnum.NO_USER.getStateInfo());
		return be;
	}

	@Override
	public Integer updateBlogUser(BlogUser blogUser) {
		return blogUserDao.updateByPrimaryKeySelective(blogUser);
	}

	@Override
	public BlogUserExecution addBlogUser(BlogUser blogUser, InputStream blogUserImgInputStream, String fileName) throws BlogUserOperationException {
		//空值判断
		if(blogUser == null){
			return new BlogUserExecution(BlogUserStateEnum.NULL_USER_INFO);
		}
//			给用户信息赋值
			blogUser.setStatus(1);
			blogUser.setCreatetime(new Date());
			blogUser.setLastEdittime(new Date());
				try {
						//存储图片
					if (blogUserImgInputStream != null) {
						addBlogUserImg(blogUser, blogUserImgInputStream, fileName);
					}
						int effectedNum = blogUserDao.insertSelective(blogUser);
						if (effectedNum > 0){
							return new BlogUserExecution(BlogUserStateEnum.NORMAL);
						}
//						//更新店铺的图片地址
//						effectedNum = BlogUserDao.updateShop(blogUser);
					return new BlogUserExecution(BlogUserStateEnum.NULL_USER_INFO);
	}
		catch (BlogUserOperationException e){
			throw new BlogUserOperationException("insertUser error: " + e.getMessage());
		}
	}

	//根据找回密码的条件找到数据库中匹配的记录
	@Override
	public UserEmailExecution getUserEmailByCondition(UserEmail userEmailCondition) {
		UserEmail userEmail1 = userEmailDao.selectByUserEmailCondition(userEmailCondition);
		UserEmailExecution ue = new UserEmailExecution();
		if (userEmail1!= null){
			ue.setUserEmail(userEmail1);
			ue.setState(UserEmailStateEnum.NORMAL.getState());
			return ue;
		}
		ue.setState(UserEmailStateEnum.NO_CODE.getState());
		ue.setStateInfo(UserEmailStateEnum.NO_CODE.getStateInfo());
		return ue;
	}

	//将生成的找回密码记录存库
	@Override
	public UserEmailExecution addUserEmail(UserEmail userEmail) {
		BlogUser blogUserCondition = new BlogUser();
		blogUserCondition.setEmail(userEmail.getEmail());
		BlogUser blogUser = blogUserDao.selectByBlogUserCondition(blogUserCondition,0,1);
		if (blogUser!=null) {
			Integer id = blogUser.getId();
			userEmail.setId(id);
		}else{
			return new UserEmailExecution(UserEmailStateEnum.NORMAL.NO_USER);
		}
		if (userEmail.getSendType() == null){
			throw new UserEmailOperationException("必须设置邮件类型,register或者forget");
		}
		userEmail.setSendTime(new Date());
		int effectedNum = userEmailDao.insert(userEmail);
		if (effectedNum > 0){
			return new UserEmailExecution(UserEmailStateEnum.NORMAL,userEmail);
		}
		return new UserEmailExecution(UserEmailStateEnum.NO_CODE);
	}

	//用户设置新密码之后删除useremail对应表中的记录
	@Override
	public Integer deleteUserEmail(UserEmail userEmail) {
		if (userEmail.getId() != null){
			Integer deleteFlag = userEmailDao.deleteByPrimaryKey(userEmail.getId());
			return deleteFlag;
		}else {
			throw new UserEmailOperationException("数据库错误,找回密码流程结束失败,请稍后再试" );
		}
	}
}
